﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class appconn_pwd : Form
    {
        public appconn_pwd()
        {
            InitializeComponent();
        }
        private byte[] _key1;
        private string _keyString1;
        private SymmetricAlgorithm _symmetricAlgorithm;
        private byte[] _key2;
        private string _keyString2;

        private void button1_Click(object sender, EventArgs e)
        {
            
            _symmetricAlgorithm = DES.Create();
            _keyString1 = this.textBox1.Text;
            _keyString2 = this.textBox2.Text;
            _key1 = CreateKeyBytes(_keyString1);
            _key2 = CreateKeyBytes(_keyString2);

            var pwd = Decode(this.textBox3.Text);
            MessageBox.Show(pwd);
        }
        private byte[] CreateKeyBytes(string text1)
        {
            string str;
            if (this._symmetricAlgorithm.LegalKeySizes.Length > 0)
            {
                int num = 0;
                int minSize = this._symmetricAlgorithm.LegalKeySizes[0].MinSize;
                while ((text1.Length * 8) > minSize)
                {
                    num = minSize;
                    minSize += this._symmetricAlgorithm.LegalKeySizes[0].SkipSize;
                }
                str = text1.PadRight(minSize / 8, ' ');
            }
            else
            {
                str = text1;
            }
            return Encoding.ASCII.GetBytes(str);
        }

        public string Decode(string text1)
        {
            if (text1 != null)
            {
                byte[] buffer = Convert.FromBase64String(text1);
                MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length);
                this._symmetricAlgorithm.Key = this._key1;
                this._symmetricAlgorithm.IV = this._key2;
                ICryptoTransform transform = this._symmetricAlgorithm.CreateDecryptor();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
                StreamReader reader = new StreamReader(stream2);
                return reader.ReadToEnd();
            }
            return text1;

        }

        private void appconn_pwd_Load(object sender, EventArgs e)
        {
        }

    }
}
